<?php
require_once('utils.php');

if(!isset($_GET['book']) || !isset($_GET['chapter']) || !isset($_GET['section'])){
    die(json_encode(array(
            'msg' => 'Content not found'
        )));
}

$book =  json_decode(file_get_contents(($_GET['book'] == 'old' ? 'old' : 'new') . '.json'), true);

$chapterIndex = keepInRange(intval($_GET['chapter']), 0, count($book['chapters']) - 1);
$chapter = $book['chapters'][$chapterIndex];

$sectionIndex = keepInRange(intval($_GET['section']), 0, count($chapter['sections']) - 1);
$section = $chapter['sections'][$sectionIndex];


$res = array(
        'name' => $book['name'],
        'chapter' => array(
            'index' => $chapterIndex,
            'count' => count($book['chapters']),
            'order' => '第' . number2Chinese($chapterIndex + 1) . '卷', // chapter 卷
            'title' => $chapter['title'],
        ),
        'section' => array(
            'index' => $sectionIndex,
            'count' => count($chapter['sections']),
            'order' => '第' . number2Chinese($sectionIndex + 1) . '章', // section 章
            'content' => $section['content'],
        )
    );

echo json_encode($res);

?>